package cn.iocoder.yudao.module.pi.service.bizlog.es;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.pi.api.BizApiLogSearchClient;
import cn.iocoder.yudao.module.pi.controller.admin.bizlog.es.dto.ApiLogQueryDTO;
import cn.iocoder.yudao.module.pi.controller.admin.bizlog.es.dto.EsignLogQueryDTO;
import cn.iocoder.yudao.module.pi.controller.admin.bizlog.es.vo.ApiLogQueryVO;
import cn.iocoder.yudao.module.pi.controller.admin.bizlog.es.vo.EsignLogQueryVO;
import com.mzt.logapi.starter.annotation.LogRecord;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static cn.iocoder.yudao.module.pi.enums.LogRecordConstants.*;

/**
 * @author shaoxh
 * @version 1.0
 * @description:
 * @date 2025/8/8 11:02
 */
@Service
public class BizApiLogSearchImpl implements BizApiLogSearch {

    @Resource
    private BizApiLogSearchClient bizApiLogSearchClient;

    @Override
    @LogRecord(type = API_LOG, subType = API_LOG_QUERY_SUB_TYPE, bizNo = "0", success = API_LOG_QUERY_SUCCESS)
    public CommonResult<PageResult<ApiLogQueryVO>> apiLogQuery(ApiLogQueryDTO apiLogQueryDTO) {
        String responseStr = bizApiLogSearchClient.post(JSONUtil.toJsonStr(apiLogQueryDTO),
                System.getenv("ES_LOG") + "/es/query");
        if (StrUtil.isNotBlank(responseStr)) {
            return JSONUtil.toBean(responseStr, new TypeReference<CommonResult<PageResult<ApiLogQueryVO>>>() {
            }, false);
        }
        return CommonResult.success(new PageResult<>(0L));
    }

    @Override
    @LogRecord(type = E_SIGN_LOG, subType = E_SIGN_LOG_QUERY_SUB_TYPE, bizNo = "0", success = E_SIGN_LOG_QUERY_SUCCESS)
    public CommonResult<PageResult<EsignLogQueryVO>> esignLogQuery(EsignLogQueryDTO esignLogQueryDTO) {
        String responseStr = bizApiLogSearchClient.post(JSONUtil.toJsonStr(esignLogQueryDTO),
                System.getenv("ES_LOG") + "/es/query-esign-log");
        if (StrUtil.isNotBlank(responseStr)) {
            return JSONUtil.toBean(responseStr, new TypeReference<CommonResult<PageResult<EsignLogQueryVO>>>() {
            }, false);
        }
        return CommonResult.success(new PageResult<>(0L));
    }
}
